package com.zhj.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhj.product.converter.ItemsCommentsConverter;
import com.zhj.product.entity.dto.ItemsCommentsDTO;
import com.zhj.product.entity.dto.ItemsCommentsLevelDTO;
import com.zhj.product.entity.dto.ItemsCommentsPageDTO;
import com.zhj.product.entity.pojo.ItemsComments;
import com.zhj.product.entity.req.ItemsCommentsPageReq;
import com.zhj.product.mapper.ItemsCommentsMapper;
import com.zhj.product.service.ItemsCommentsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ItemsCommentsServiceImpl implements ItemsCommentsService{

    @Resource
    private ItemsCommentsMapper itemsCommentsMapper;

    @Override
    public List<ItemsComments> selectCommentsByItemId(String itemId) {
        LambdaQueryWrapper<ItemsComments> queryWrapper = new QueryWrapper<ItemsComments>().lambda().eq(ItemsComments::getItemId, itemId);
        return itemsCommentsMapper.selectList(queryWrapper);
    }

    /**
     * 分页查询评论数
     * @param itemsCommentsPageReq
     * @return
     */
    @Override
    public ItemsCommentsPageDTO selectItemsCommentsPage(ItemsCommentsPageReq itemsCommentsPageReq) {
        Page<ItemsComments> commentsPage = new Page<>(itemsCommentsPageReq.getPage(), itemsCommentsPageReq.getPageSize());
        IPage<ItemsComments> itemsCommentsPage = itemsCommentsMapper.selectItemsCommentsPage(commentsPage, itemsCommentsPageReq);
        List<ItemsCommentsDTO> itemsCommentsDTOS = ItemsCommentsConverter.INSTANCE.poToDto(itemsCommentsPage.getRecords());
        //// TODO: 2022/5/3 查询用户的昵称和头像 ，需要使用 DesensitizationUtil 进行脱敏，现在还需集成用户中心
        return ItemsCommentsPageDTO
                .builder()
                .rows(itemsCommentsDTOS)
                //总记录数
                .records(itemsCommentsPage.getTotal())
                //总页数
                .total(itemsCommentsPage.getPages()).build();

    }

    /**
     * 查询评论数量
     * @param itemId
     * @return
     */
    @Override
    public ItemsCommentsLevelDTO selectItemsCommentsLevel(String itemId) {
        LambdaQueryWrapper<ItemsComments> queryWrapper = new QueryWrapper<ItemsComments>()
                .lambda()
                .eq(ItemsComments::getItemId, itemId);
        List<ItemsComments> itemsComments = itemsCommentsMapper.selectList(queryWrapper);
        Map<Integer, Long> collect = itemsComments
                .stream()
                .collect(Collectors.groupingBy(ItemsComments::getCommentLevel, Collectors.counting()));
        return ItemsCommentsLevelDTO
                .builder()
                .totalCounts((long) itemsComments.size())
                .goodCounts(collect.get(1))
                .normalCounts(collect.get(2))
                .badCounts(collect.get(3)).build();
    }
}
